﻿import mx.remoting.PendingCall;
import mx.remoting.Service;
import mx.rpc.FaultEvent;
import mx.rpc.RelayResponder;
import mx.rpc.ResultEvent;

import com.customine.api.ICustomDesign;
import com.customine.api.ICustomProduct;
import com.customine.BlankTShirt;
import com.customine.Constants;
import com.customine.design.SingleLineDesign;
import com.customine.shop.TshirtCustomize;

/**
 * @author colin
 */
 
class com.customine.Search {
	private var _service : Service;

	private var listAllProductHandler : Function;
	private var listLastestProductHandler : Function;
	private var listProductHandler : Function;

	private var getCatHandler : Function;

	public function Search(){
		_service = new Service(Constants.REMOTE_GATEWAY, null, Constants.REMOTE_SERVICE, null , null);
	}

	public function listAllProduct(listAllProductHandler:Function){
		var pc:PendingCall = _service.AllProductList(0,999);
		pc.responder = new RelayResponder(this, "listAllProductResult", "handleError");
		this.listAllProductHandler = listAllProductHandler;
	}

	public function listLatestProduct(listLastestProductHandler:Function){
		var pc:PendingCall = _service.LatestProductList();
		pc.responder = new RelayResponder(this, "listLatestProductResult", "handleError");
		this.listLastestProductHandler = listLastestProductHandler;
	}

	public function listProductByCat(listProductHandler:Function,catID:Number){
		var pc:PendingCall = _service.ProductByCat(catID);
		pc.responder = new RelayResponder(this, "listProductResult", "handleError");
		this.listProductHandler = listProductHandler;
	}

	public function getCat(getCatHandler:Function,parentCatID:Number){
		var pc:PendingCall;
		if(parentCatID==undefined){
			pc = _service.ParentCat();
		}else{
			pc = _service.SubCat(parentCatID);
		}
		pc.responder = new RelayResponder(this, "getCatResult", "handleError");
		this.getCatHandler = getCatHandler;
	}

	private function getCatResult(re:ResultEvent){
		getCatHandler(catResultHandler(re));
	}

	private function listAllProductResult(re:ResultEvent){
		listAllProductHandler(resultHandler(re));
	}

	private function listLatestProductResult(re:ResultEvent){
		listLastestProductHandler(resultHandler(re));
	}

	private function listProductResult(re:ResultEvent){
		listProductHandler(resultHandler(re));
	}

	private function resultHandler(re:ResultEvent):Array{
		//product_id,product_name,quantity,designer,description,price,time_created,time_modified,time_available,enable,line1_max,line2_max,product_id,assets_id,front_rear,id,picture,type_id
		var rs = re.result;
		var col = rs.items;

//facade product from server to local.

		var ret:Array = new Array();
		for(var i=0;i<col.length;i++){
			// change Custom Design Here.
			var prd = col[i];
			//if add new design type, extend the factory method here
			var design:ICustomDesign = new SingleLineDesign(prd.line1_max,prd.picture);
			var prototype:ICustomProduct = new TshirtCustomize(col.product_id,col.price,String(1),1,String(1),design);

			var editableItem = new BlankTShirt(prototype,prd);
			ret.push(editableItem);
		}
		return ret;
	}

	private function handleError(fe:FaultEvent){
		throw new Error('Remote connection error:'+fe.fault.description);
	}

	private function catResultHandler(re:ResultEvent) : Array {
		//return result for comboBox data provider
		var items = re.result.items;
		var ret:Array = [{data:null,label:'Please Select'}];
		for(var i=0;i<items.length;i++){
			var item = items[i]; 
			ret.push({data:item.category_id,label:item.category_name});
//			for(var name in item){trace(name+":"+item[name]);}
		}
		return ret;
	}
}